import axios from 'axios'
import { useAuthStore } from '@/stores/auth'

const instance = axios.create({
  baseURL: '/api',
  timeout: 15000,
})

instance.interceptors.request.use((config) => {
  try {
    const store = useAuthStore()
    const token = store.token
    if (token) {
      config.headers = config.headers || {}
      ;(config.headers as any)['Authorization'] = `Bearer ${token}`
    }
  } catch {}
  return config
})

instance.interceptors.response.use(
  (res) => res,
  (error) => Promise.reject(error)
)

export default instance


